package event

import (
	//"fmt"
	//"iot-base/common/cache"
	"iot-base/common/cache"
	ca "iot-base/common/cache/alarm"
	"iot-base/common/eventtype"
	"time"
)

func removeID(ids []string, id string) []string {
	var result []string
	for i, tid := range ids {
		if tid == id {
			result = append(ids[:i], ids[i+1:]...)
			return result
		}
	}
	return ids
}

//SyncAlarms 同步告警，使用时传入设备id和目前设备的告警类型
func SyncAlarms(ids []string, alarms []*eventtype.Alarm) {

	now := time.Now().Unix()

	//目标告警多于实际告警，则发送告警
	//目标告警少于实际告警，则发送告警消除
	//建立一个目标告警的map
	//遍历实际告警，在目标中存在则将目标告警map中的数据删除，如果没有找到，发送告警消除
	//遍历目标告警map中剩余的告警发送告警
	//currentIDs := ids
	mapTarget := make(map[eventtype.AlarmKey]*eventtype.Alarm)
	mapChecked := make(map[string]bool)
	//mapOfflineDev := make(map[string]bool)
	noAlarmDev := make([]string, len(ids))
	onlineDev := make([]string, len(ids))
	copy(noAlarmDev, ids)
	copy(onlineDev, ids)
	for i := range alarms {
		pTarget := alarms[i]

		if pTarget.AlarmID == "offline" { //设备断线，此时其他告警都无效 //修改为断线也同步告警 2022.1.11 zhang
			ThingAlarm(pTarget)
			//mapOfflineDev[pTarget.AlarmKey.TID] = true
			onlineDev = removeID(onlineDev, pTarget.AlarmKey.TID)
		}
		mapTarget[pTarget.AlarmKey] = pTarget
		noAlarmDev = removeID(noAlarmDev, pTarget.AlarmKey.TID)

	}

	var curTid string
	for _, pTarget := range alarms {

		if curTid != pTarget.AlarmKey.TID {
			checked, ok := mapChecked[pTarget.AlarmKey.TID]
			curTid = pTarget.AlarmKey.TID
			if checked && ok {
				continue
			}

			mapChecked[pTarget.AlarmKey.TID] = true
			//curKey = pTarget.AlarmKey
			recordAlarms, _, _ := ca.GetThingAlarmsCondition(pTarget.AlarmKey.TID, true)
			if len(recordAlarms) == 0 { //当前没有告警
				for _, pAlarm := range mapTarget {
					if pAlarm.AlarmKey.TID == curTid {
						ThingAlarm(pAlarm)
						delete(mapTarget, pAlarm.AlarmKey)
					}

				}
				continue
			}
			//offline, found := mapOfflineDev[curTid]

			for _, pRecordAlarm := range recordAlarms {
				//:= alarms[j]
				_, ok := mapTarget[pRecordAlarm.AlarmKey]
				if !ok {
					//if !offline || !found { //没有断线的话，则消除其他告警
					pRecordAlarm.Flag = "END"
					pRecordAlarm.ClearTime = now
					pRecordAlarm.Checker = "syncAlarms-1"
					ThingAlarmClear(&pRecordAlarm)
					//}

				} else {
					delete(mapTarget, pRecordAlarm.AlarmKey)
				}
			}

		}

	}

	for _, pAlarmTarget := range mapTarget {
		ThingAlarm(pAlarmTarget)
	}

	for _, id := range noAlarmDev {
		recordAlarms := ca.GetThingAlarms(id)
		for _, pRecordAlarm := range recordAlarms {
			pRecordAlarm.Flag = "END"
			pRecordAlarm.ClearTime = now
			pRecordAlarm.Checker = "syncAlarms-2"
			ThingAlarmClear(&pRecordAlarm)
		}
	}
	alarm := eventtype.Alarm{}
	for _, id := range onlineDev {
		//fmt.Println("found-----------dev----------online-----------", id)
		alarm.TID = id
		alarm.AlarmID = "offline"
		alarm.ClearTime = now
		//alarm.Checker = "sync alarm"
		alarm.Flag = "END"
		alarm.Checker = "syncAlarms-3"
		ThingAlarmClear(&alarm)
	}

}

//SyncAlarmsByEdge 同步告警
func SyncAlarmsByEdge(ids []string, alarms []eventtype.AlarmState) {
	//mapOldAlarms := make(map[eventtype.AlarmKey](eventtype.AlarmState))
	//mapOldClears := make(map[eventtype.AlarmKey](eventtype.AlarmState))
	//mapNewAlarms := make(map[eventtype.AlarmKey](*eventtype.AlarmState))
	//mapNewClears := make(map[eventtype.AlarmKey](*eventtype.AlarmState))

	now := time.Now().Unix()
	var oldAlarm eventtype.AlarmState
	for _, alarm := range alarms {
		pAlarm := &alarm
		isAlarm := ca.GetAlarmObj(pAlarm.TID, pAlarm.AlarmID, pAlarm.Mindex, &oldAlarm)
		if pAlarm.IsAlarm {
			//var oldAlarm eventtype.AlarmState
			if isAlarm {
				if pAlarm.IsHide == oldAlarm.IsHide && pAlarm.TimeChanged == oldAlarm.TimeChanged {
					continue
				}
			}
			tid := pAlarm.TID
			aid := pAlarm.AlarmID
			pThing, _ := cache.GetThing(tid)
			if pThing == nil {
				return
			}
			_, pInfo := cache.GetEventInfo(pThing.Thing.ModelID, aid)
			if pInfo != nil {
				pAlarm.AlarmName = pInfo.Name
			}
			ca.SaveAlarmDirect(pAlarm)
		} else {
			if !isAlarm {
				continue
			}
			ca.SaveClearDirect(pAlarm, now)
		}

	}

}
